﻿using DevExpress.XtraGrid.Views.BandedGrid;
using Newtonsoft.Json;
using NursingFormSystem.Model;
using NursingFormSystem.NurFormsService;
using ShiftProject.Scale;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NursingFormSystem.Scale
{
    public partial class HierarchicalHistory : Form
    {
        #region 变量声明
        NursingFormRecord record = new NursingFormRecord();
        NursingForm formService = new NursingForm();
        Tb_User LoginUser;
        PatientInfo PatientInfo;
        #endregion

        #region 构造函数
        public HierarchicalHistory(Tb_User loginUser, PatientInfo patientInfo)
        {
            InitializeComponent();
            this.PatientInfo = patientInfo;
            this.LoginUser = loginUser;
            BindControl();
            BindInfo();
        }
        #endregion

        #region GridView设置
        private void BindControl()
        {
            dateStart.DateTime = DateTime.Now.AddDays(-30).Date;
            dateEnd.DateTime = DateTime.Now;

            foreach (GridBand gridBand in this.bandedGridView3.Bands)//设置居中
            {
                foreach (GridBand gridBandSecond in gridBand.Children)
                {
                    gridBandSecond.OptionsBand.FixedWidth = true;
                    gridBandSecond.AppearanceHeader.Options.UseTextOptions = true;
                    gridBandSecond.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
                    gridBandSecond.AppearanceHeader.TextOptions.VAlignment = DevExpress.Utils.VertAlignment.Center;
                }
                gridBand.OptionsBand.FixedWidth = true;
                gridBand.AppearanceHeader.Options.UseTextOptions = true;
                gridBand.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
                gridBand.AppearanceHeader.TextOptions.VAlignment = DevExpress.Utils.VertAlignment.Center;
            }
        }
        #endregion

        #region LoadData显示
        private void BindInfo()
        {
            if (this.PatientInfo == null) return;
            string patientid = this.PatientInfo.PATIENT_ID;//住院号
            int VisitId = PatientInfo.VISIT_ID;//住院次数
            int FormId = (int)FormType.PatientCare;
            var ret = formService.GetNursingFormRecordByDate(patientid, VisitId, FormId, dateStart.DateTime, dateEnd.DateTime);
            DataTable dt = new DataTable();
            dt.Columns.Add("Eat1", typeof(string));
            dt.Columns.Add("Eat2", typeof(string));
            dt.Columns.Add("Eat3", typeof(string));
            dt.Columns.Add("Bathe1", typeof(string));
            dt.Columns.Add("Bathe2", typeof(string));
            dt.Columns.Add("Decorate1", typeof(string));
            dt.Columns.Add("Decorate2", typeof(string));
            dt.Columns.Add("Dressing1", typeof(string));
            dt.Columns.Add("Dressing2", typeof(string));
            dt.Columns.Add("Dressing3", typeof(string));
            dt.Columns.Add("ControlStool1", typeof(string));
            dt.Columns.Add("ControlStool2", typeof(string));
            dt.Columns.Add("ControlStool3", typeof(string));
            dt.Columns.Add("ControlUrination1", typeof(string));
            dt.Columns.Add("ControlUrination2", typeof(string));
            dt.Columns.Add("ControlUrination3", typeof(string));
            dt.Columns.Add("GoToilet1", typeof(string));
            dt.Columns.Add("GoToilet2", typeof(string));
            dt.Columns.Add("GoToilet3", typeof(string));
            dt.Columns.Add("BedChairTransfer1", typeof(string));
            dt.Columns.Add("BedChairTransfer2", typeof(string));
            dt.Columns.Add("BedChairTransfer3", typeof(string));
            dt.Columns.Add("BedChairTransfer4", typeof(string));
            dt.Columns.Add("LevelWalk1", typeof(string));
            dt.Columns.Add("LevelWalk2", typeof(string));
            dt.Columns.Add("LevelWalk3", typeof(string));
            dt.Columns.Add("LevelWalk4", typeof(string));
            dt.Columns.Add("StairActivity1", typeof(string));
            dt.Columns.Add("StairActivity2", typeof(string));
            dt.Columns.Add("StairActivity3", typeof(string));
            dt.Columns.Add("ZongFen", typeof(string));
            dt.Columns.Add("SelfCareGrade1", typeof(string));
            dt.Columns.Add("SelfCareGrade2", typeof(string));
            dt.Columns.Add("SelfCareGrade3", typeof(string));
            dt.Columns.Add("SelfCareGrade4", typeof(string));
            dt.Columns.Add("IllnessGrade1", typeof(string));
            dt.Columns.Add("IllnessGrade2", typeof(string));
            dt.Columns.Add("IllnessGrade3", typeof(string));
            dt.Columns.Add("HuLiJiBie", typeof(string));
            dt.Columns.Add("ZenRenHuShiQianMing", typeof(string));
            dt.Columns.Add("RiQi", typeof(string));
            dt.Columns.Add("Id", typeof(string));
            if (ret.Code == 1)
            {
                List<NursingFormRecord> list = JsonConvert.DeserializeObject<List<NursingFormRecord>>(ret.D);
                list = list.OrderByDescending(p => p.AssessmentDate).ToList();
                int[] eatArray = { 10, 5, 0 };
                int[] batheArray = { 5, 0 };
                int[] decorateArray = { 5, 0 };
                int[] dressingArray = { 10, 5, 0 };
                int[] controlStoolArray = { 10, 5, 0 };
                int[] controlUrinationArray = { 10, 5, 0 };
                int[] goToiletArray = { 10, 5, 0 };
                int[] bedChairTransfer = { 15, 10, 5, 0 };
                int[] levelWalkArray = { 15, 10, 5, 0 };
                int[] stairActivityArray = { 10, 5, 0 };
                foreach (NursingFormRecord record in list)
                {
                    var NursingFormData = record.NursingFormData;
                    NursingGrade xmnr = new NursingGrade();
                    xmnr = XmlUtil.XMLToObject<NursingGrade>(NursingFormData, Encoding.UTF8);
                    DataRow row = dt.NewRow();
                    //进食
                    string Eat = xmnr.JinShi;
                    if (!string.IsNullOrEmpty(Eat))
                    {
                        int n = Convert.ToInt32(Eat);
                        row["Eat" + (n + 1)] = eatArray[n];
                    }
                    //洗澡
                    string Bathe = xmnr.XiZao;
                    if (!string.IsNullOrEmpty(Bathe))
                    {
                        int n = Convert.ToInt32(Bathe);
                        row["Bathe" + (n + 1)] = batheArray[n];
                    }
                    //修饰
                    string Decorate = xmnr.XiuShi;
                    if (!string.IsNullOrEmpty(Decorate))
                    {
                        int n = Convert.ToInt32(Decorate);
                        row["Decorate" + (n + 1)] = decorateArray[n];
                    }
                    //穿衣
                    string Dressing = xmnr.ChuangYi;
                    if (!string.IsNullOrEmpty(Dressing))
                    {
                        int n = Convert.ToInt32(Dressing);
                        row["Dressing" + (n + 1)] = dressingArray[n];
                    }
                    //控制大便
                    string ControlStool = xmnr.KongZhiDaBian;
                    if (!string.IsNullOrEmpty(ControlStool))
                    {
                        int n = Convert.ToInt32(ControlStool);
                        row["ControlStool" + (n + 1)] = controlStoolArray[n];
                    }
                    //控制小便
                    string ControlUrination = xmnr.KongZhiXiaoBian;
                    if (!string.IsNullOrEmpty(ControlUrination))
                    {
                        int n = Convert.ToInt32(ControlUrination);
                        row["ControlUrination" + (n + 1)] = controlUrinationArray[n];
                    }
                    //如厕
                    string GoToilet = xmnr.RuCe;
                    if (!string.IsNullOrEmpty(GoToilet))
                    {
                        int n = Convert.ToInt32(GoToilet);
                        row["GoToilet" + (n + 1)] = goToiletArray[n];
                    }
                    //床椅转移
                    string BedChairTransfer = xmnr.ChuangYiZhuangYi;
                    if (!string.IsNullOrEmpty(BedChairTransfer))
                    {
                        int n = Convert.ToInt32(BedChairTransfer);
                        row["BedChairTransfer" + (n + 1)] = bedChairTransfer[n];
                    }
                    //平地行走
                    string LevelWalk = xmnr.PingDiXingZou;
                    if (!string.IsNullOrEmpty(LevelWalk))
                    {
                        int n = Convert.ToInt32(LevelWalk);
                        row["LevelWalk" + (n + 1)] = levelWalkArray[n];
                    }
                    //上下楼梯
                    string StairActivity = xmnr.ShangXiaLouTi;
                    if (!string.IsNullOrEmpty(StairActivity))
                    {
                        int n = Convert.ToInt32(StairActivity);
                        row["StairActivity" + (n + 1)] = stairActivityArray[n];
                    }
                    //自理能力等级
                    string SelfCareGrade = xmnr.ZiLiNengLi;
                    if (!string.IsNullOrEmpty(SelfCareGrade))
                    {
                        int n = Convert.ToInt32(SelfCareGrade);
                        row["SelfCareGrade" + (n + 1)] = "√";
                    }
                    //病情等级
                    string IllnessGrade = xmnr.BingQingDengJi;
                    if (!string.IsNullOrEmpty(IllnessGrade))
                    {
                        int n = Convert.ToInt32(IllnessGrade);
                        row["IllnessGrade" + (n + 1)] = "√";
                    }
                    //总分
                    row["ZongFen"] = xmnr.ZongFen;
                    //护理级别
                    row["HuLiJiBie"] = xmnr.HuLiJiBie;
                    //责任护士签名
                    row["ZenRenHuShiQianMing"] = xmnr.ZenRenHuShiQianMing;
                    //日期
                    string RiQi = xmnr.RiQi;
                    DateTime date = Convert.ToDateTime(RiQi);
                    row["RiQi"] = date.ToString("yyyy-MM-dd");
                    //ID隐藏，双击触发事件
                    row["Id"] = record.Id;
                    dt.Rows.Add(row);
                }
            }
            gridControl1.DataSource = dt;
        }
        #endregion

        #region 查询
        private void simpleButtonchaxun_Click(object sender, EventArgs e)
        {
            BindInfo();
        }
        #endregion

        #region 双击事件
        private void bandedGridView3_DoubleClick(object sender, EventArgs e)
        {
            ShowEditForm();
        }
        #endregion

        #region 新增
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            AddHierarchical addh = new AddHierarchical(LoginUser, PatientInfo, string.Empty);
            addh.ShowDialog();
            if (addh.DialogResult == DialogResult.OK)
            {
                BindInfo();
            }
        }
        #endregion

        #region 修改
        private void simpleButton2_Click(object sender, EventArgs e)
        {
            ShowEditForm();
        }

        private void ShowEditForm()
        {
            if (bandedGridView3.RowCount > 0)
            {
                string Id = bandedGridView3.GetRowCellValue(bandedGridView3.FocusedRowHandle, "Id").ToString();
                AddHierarchical addForm = new AddHierarchical(LoginUser, PatientInfo, Id);
                addForm.ShowDialog();
                if (addForm.DialogResult == DialogResult.OK)
                {
                    BindInfo();
                }
            }
        }
        #endregion

        #region 打印
        private void simpleButtondayin_Click(object sender, EventArgs e)
        {
            HierarchicalPrint h = new HierarchicalPrint(PatientInfo, dateStart.DateTime, dateEnd.DateTime);
            h.Show();
        }
        #endregion

        #region 删除
        private void simpleButton1_Click_1(object sender, EventArgs e)
        {
            if (bandedGridView3.RowCount <= 0) return;
            if (MessageBox.Show("确定要删除数据吗？", "删除前确认", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                string id = bandedGridView3.GetRowCellValue(bandedGridView3.FocusedRowHandle, "Id").ToString();
                var ret2 = formService.DeleteNursingFormRecord(id);
                MessageBox.Show(ret2.M);
                if (ret2.Code == 1)
                {
                    BindInfo();
                }
            }
        }
        #endregion
    }
}